package geektime.concurrent.part3;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 演示CompletableFuture的supplyAsync和thenApply方法
 */
public class SupplyAsyncDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<String> completableFuture = CompletableFuture
            .supplyAsync(() -> getDataById(100)).thenApply(data -> sendData(data));
        System.out.println("get final result: " + completableFuture.get());
    }

    private static String getDataById(Integer id) {
        System.out.println("getDataById is executed by: " + Thread.currentThread().getName());
        return "data:" + id;
    }

    private static String sendData(String data) {
        System.out.println("sendData is executed by: " + Thread.currentThread().getName());
        System.out.println(data);
        return data;
    }
}
